/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parallel.Preparser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;

/**
 *
 * @author zwirek
 */
public class Preparser {

    private enum Mode {

        TEXT, ENTITY, START_TAG, END_TAG, LT, ATTRIBUTE_LVALUE,
        ATTRIBUTE, ATTRIBUTE_RVALUE, QUOTE, IN_TAG, SINGLE_TAG, COMMENT, DONE,
        DOCTYPE, PRE, CDATA_COMMENT
    }

    public static int bufferSize = 250;
    public ArrayList<Integer> nodePositions;
    Reader r;
    long size;
    Mode mode;

    public Preparser(String filename) throws FileNotFoundException {
        File f = new File(filename);
        size = f.length();
        r = new FileReader(f);
        nodePositions = new ArrayList<Integer>();
    }

    public void preparse() throws IOException {
        int startTag=0;
        int start = 0;
        mode = Mode.TEXT;
        StringBuffer sb = new StringBuffer();
        while (start != size) {//czytamy cały plik do bufora
            int actualBufferSize = (int) Math.min(bufferSize, size - start);
            char[] readBuffer = new char[actualBufferSize];
            r.read(readBuffer);
            for (int i = 0; i < actualBufferSize; i++) {
                int c = readBuffer[i];
                switch(mode){
                    case TEXT:{
                        if(c=='<'){
                            startTag=start+i;
                            mode = Mode.LT;
                        }

                        break;
                    }
                    case LT:{
                        if(c=='/'){
                            mode = Mode.END_TAG;
                        }else if(c=='?'){
                            mode = Mode.DOCTYPE;
                        }else if(c=='!'){
                            mode = Mode.CDATA_COMMENT;
                        }else{
                            mode = Mode.START_TAG;
                        }

                        break;
                    }
                    case START_TAG:{
                        if(c=='/'){
                            mode = Mode.SINGLE_TAG;
                        }else if(c=='\"'||c=='\"'){
                            mode = Mode.ATTRIBUTE;
                        }else if(c=='>'){
                            nodePositions.add(startTag);
                            mode = Mode.TEXT;
                        }
                        break;
                    }
                    case  SINGLE_TAG:{
                        if(c=='>'){
                            nodePositions.add(startTag);
                            mode = Mode.TEXT;
                        }
                        break;
                    }
                    case DOCTYPE:{
                        if (c == '>'){
                            mode = Mode.TEXT;
                        } else {
                            sb.append((char) c);
                        }
                    
                        break;
                    }
                    case CDATA_COMMENT:{
                        if (c == '>'
                                && (sb.toString().endsWith("]]")||sb.toString().endsWith("--"))) {
                            sb.setLength(0);
                            mode = Mode.TEXT;
                        } else {
                            sb.append((char) c);
                        }
                    
                        break;
                    }
                    case ATTRIBUTE:{
                        if(c=='\"'||c=='\"'){
                            mode = Mode.START_TAG;
                        }
                        break;
                    }
                    case END_TAG:{
                        if(c=='>'){
                            mode = Mode.TEXT;
                        }
                        break;
                    }
                }
            }
            start += actualBufferSize;
        }
    }
}
